package com.wlm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlm.dao.ItemCatMapper;
import com.wlm.pojo.ItemCat;
import com.wlm.service.ItemCatService;
import com.wlm.vo.EasyUITree;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Resource
    private ItemCatMapper itemCatMapper;

    @Override
    public ItemCat findItemCatById(Long itemCatId) {
        return itemCatMapper.selectById(itemCatId);
    }

    @Override
    public List<EasyUITree> findEasyUITree(Long parentId) {
        List<ItemCat> itemCatList = findItemCatByParentId(parentId);
        List<EasyUITree> treeList = new ArrayList<EasyUITree>();

        for(ItemCat itemCat : itemCatList) {
            Long id = itemCat.getId();
            String text = itemCat.getName();
            String state = itemCat.getIsParent() ? "closed" : "open";

            EasyUITree easyUITree = new EasyUITree(id, text, state);
            treeList.add(easyUITree);
        }

        return treeList;
    }

    public List<ItemCat> findItemCatByParentId(Long parentId) {
        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<ItemCat>();
        queryWrapper.eq("parent_id", parentId);
        List<ItemCat> itemCatList = itemCatMapper.selectList(queryWrapper);
        return itemCatList;
    }

}
